﻿/*
 * ========================================================================
 * Copyright(c) 2007-2012 情留メ蚊子, All Rights Reserved.
 * Welcom use the LiteFetion.
 * See more information,Please goto http://www.94qing.com
 * ========================================================================
 * 
 * 作 者：情留メ蚊子
 * Q  Q: 540644769
 * 邮 箱：qlwz@qq.com
 * 网 址：http://www.94qing.com
 * ========================================================================
*/
using System;
using System.Security.Cryptography;
using System.Text;
using Newtonsoft.Json.Linq;

namespace LiteFetion.Utils
{
    public class Util
    {
        /// <summary>
        /// 得到格林威治时间
        /// </summary>
        /// <param name="dateTime">时间参数</param>
        /// <returns>格林威治时间</returns>
        public static long GetTime(DateTime dateTime)
        {
            DateTime startDate = new DateTime(1970, 1, 1);
            DateTime endDate = dateTime.ToUniversalTime();
            TimeSpan span = endDate - startDate;
            return (long)(span.TotalMilliseconds + 0.5);
        }

        private static DateTime baseDateTime = DateTime.Parse("1970-1-01 08:00:00.000");
        public static DateTime GetDateTime(int millis)
        {
            if (millis < 1000000000)
            {
                return DateTime.Now;
            }
            long i = (long)millis * 1000;
            return baseDateTime.AddTicks(i * TimeSpan.TicksPerMillisecond);
        }

        public static int GetTimeMillis(DateTime dateTime)
        {
            return (int)((dateTime - baseDateTime).TotalMilliseconds / 1000);
        }

        public static JArray GetArrayResult(string result)
        {
            try
            {
                return JArray.Parse(result);
            }
            catch
            {
                return new JArray();
            }
        }

        public static JObject GetObjectResult(string result)
        {
            try
            {
                return JObject.Parse(result);
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// 当前路径
        /// </summary>
        /// <param name="strPath"></param>
        /// <returns></returns>
        public static string MapPath(string strPath)
        {
            strPath = strPath.Replace("/", "\\");
            if (strPath.StartsWith("\\"))
            {
                strPath = strPath.TrimStart('\\');
            }
            return System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, strPath);
        }

        #region 密码加密
        private static string Binl2Hex(byte[] buffer)
        {
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < buffer.Length; i++)
            {
                builder.Append(buffer[i].ToString("x2"));
            }
            return builder.ToString().ToUpper();
        }

        private static string Md53(string input)
        {
            using (MD5 md = MD5.Create())
            {
                byte[] buffer = md.ComputeHash(Encoding.Default.GetBytes(input));
                buffer = md.ComputeHash(buffer);
                buffer = md.ComputeHash(buffer);
                return Binl2Hex(buffer);
            }
        }

        private static string Md5(string input)
        {
            byte[] buffer = MD5.Create().ComputeHash(Encoding.Default.GetBytes(input));
            return Binl2Hex(buffer);
        }

        /// <summary>
        /// 获得加密后的密码
        /// </summary>
        /// <param name="password">原始密码</param>
        /// <param name="verifycode">验证码</param>
        /// <returns></returns>
        public static string GetPassword(string password, string verifycode)
        {
            return Md5(Md53(password) + verifycode.ToUpper());
        }
        #endregion

        #region 字符串转换
        /// <summary>
        /// 字符串转布尔值
        /// </summary>
        /// <param name="strVal">字符串</param>
        /// <returns></returns>
        public static bool ParseBool(string strVal)
        {
            return ParseBool(strVal, false);
        }

        /// <summary>
        /// 字符串转布尔值
        /// </summary>
        /// <param name="strVal">字符串</param>
        /// <returns></returns>
        public static bool ParseBool(string strVal, bool defVal)
        {
            if (string.IsNullOrEmpty(strVal))
            {
                return defVal;
            }
            if (string.Compare(strVal, "true", true) == 0 || ParseInt(strVal) > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 字符串转日期
        /// </summary>
        /// <param name="strVal">字符串</param>
        /// <returns></returns>
        public static DateTime ParseDate(string strVal)
        {
            if (string.IsNullOrEmpty(strVal))
            {
                return DateTime.Now;
            }
            DateTime now;
            if (!DateTime.TryParse(strVal, out now))
            {
                now = DateTime.Now;
            }
            return now;
        }

        /// <summary>
        /// 字符串转数字
        /// </summary>
        /// <param name="strVal">字符串</param>
        /// <returns></returns>
        public static uint ParseUint(object strVal)
        {
            return ParseUint(strVal.ToString(), 0);
        }

        /// <summary>
        /// 字符串转数字
        /// </summary>
        /// <param name="strVal">字符串</param>
        /// <param name="index">默认数字</param>
        /// <returns></returns>
        public static uint ParseUint(string strVal, uint index)
        {
            if (string.IsNullOrEmpty(strVal))
            {
                return index;
            }
            uint i;
            if (uint.TryParse(strVal, out i))
            {
                return i;
            }
            return index;
        }

        /// <summary>
        /// 字符串转数字
        /// </summary>
        /// <param name="strVal">字符串</param>
        /// <returns></returns>
        public static int ParseInt(object strVal)
        {
            return ParseInt(strVal.ToString(), 0);
        }

        /// <summary>
        /// 字符串转数字
        /// </summary>
        /// <param name="strVal">字符串</param>
        /// <param name="index">默认数字</param>
        /// <returns></returns>
        public static int ParseInt(string strVal, int index)
        {
            if (string.IsNullOrEmpty(strVal))
            {
                return index;
            }
            int i;
            if (int.TryParse(strVal, out i))
            {
                return i;
            }
            return index;
        }

        /// <summary>
        /// 字符串转字符串（去前后空格）
        /// </summary>
        /// <param name="strVal">字符串</param>
        /// <returns></returns>
        public static string ParseStr(string strVal)
        {
            if (!string.IsNullOrEmpty(strVal))
            {
                strVal = strVal.Trim();
            }
            return string.Empty;
        }
        #endregion

    }
}
